"""
Case Type   : GUC
Case Name   : 使用ALTER SYSTEM SET修改数据库参数auth_iteration_count
Create At   : 2020/11/17
Owner       : opentestcase037
Description : 1、查看auth_iteration_count默认值；
              source /opt/opengauss810/env
              gs_guc check -D {cluster/dn1} -c auth_iteration_count
              2、使用ALTER SYSTM SET修改数据库参数auth_iteration_count;
              ALTER SYSTEM set auth_iteration_count to 11111;
              3、重启使其生效，校验预期结果；
Expect      : 1、显示默认值；
              2、参数修改成功；
              3、重启成功，参数修改成功。
History     :
"""

import unittest


from yat.test import macro
from yat.test import Node
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH

COMMONSH = CommonSH('PrimaryDbUser')


class GucTest(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.constant = Constant()
        self.log.info('==Guc_Connectionauthentication_Case0143开始==')
        self.db_user_node = Node(node='PrimaryDbUser')
        status = COMMONSH.get_db_cluster_status()
        self.assertTrue("Normal" in status or "Degraded" in status)

    def test_startdb(self):
        self.log.info("查询该参数默认值")
        result = COMMONSH.execute_gsguc(
            'check', '10000', 'auth_iteration_count')
        self.assertTrue(result)
        self.log.info("设置auth_iteration_count")
        sql_cmd = COMMONSH.execut_db_sql(
            f'''ALTER SYSTEM set auth_iteration_count to 11111;''')
        self.log.info(sql_cmd)
        self.assertIn("ALTER SYSTEM SET", sql_cmd)
        self.log.info("重启使其生效，并校验预期结果")
        COMMONSH.restart_db_cluster()
        checksql = f"source {macro.DB_ENV_PATH};" \
                   f"gsql -d {self.db_user_node.db_name} " \
                   f"-p {self.db_user_node.db_port} " \
                   f"-c 'show auth_iteration_count';"
        self.log.info(checksql)
        checkresult = self.db_user_node.sh(checksql).result()
        self.assertIn('10000', checkresult)

    def tearDown(self):
        self.log.info("恢复默认值")
        result = COMMONSH.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG,
                                        f'auth_iteration_count=10000')
        self.log.info(result)
        COMMONSH.restart_db_cluster()
        status = COMMONSH.get_db_cluster_status()
        self.assertTrue("Normal" in status or "Degraded" in status)
        self.log.info('==Guc_Connectionauthentication_Case0143完成==')
